package com.mall.shopping.services;/*
 *  @Author tsuki
 *  @Date 2021/4/22
 *  @Description
 *
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@Service
@Slf4j
public class ProductServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        request.requestCheck();
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        //Item为DBObject
        try {
            Item item = itemMapper.selectByPrimaryKey(request.getId().longValue());
            ProductDetailDto productDetailDto = new ProductDetailDto();
            productDetailDto.setProductId(request.getId().longValue());
            productDetailDto.setProductName(item.getTitle());
            productDetailDto.setSubTitle(item.getSellPoint());
            productDetailDto.setSalePrice(item.getPrice());
            productDetailDto.setLimitNum(item.getLimitNum() == null ? item.getNum().longValue() : item.getLimitNum().longValue());
            if (StringUtils.isNotBlank(item.getImage())) {
                productDetailDto.setProductImageBig(item.getImageBig());
                productDetailDto.setProductImageSmall(Arrays.asList(item.getImages()));
            }
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId().longValue());
            productDetailDto.setDetail(itemDesc.getItemDesc());
            productDetailResponse.setProductDetailDto(productDetailDto);
        } catch (Exception e) {
            log.error("ProductServiceImpl.getProductDetail Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(productDetailResponse, e);
        }
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        request.requestCheck();
        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        PageHelper.startPage(request.getPage(), request.getSize());

        String column = "created";
        String dir = "desc";
        if ("1".equals(request.getSort())) {//升序
            column = "price";
            dir = "asc";
        } else if ("-1".equals(request.getSort())) {
            column = "price";
        }
        List<Item> items = null;
        PageInfo<Item> itemPageInfo = null;
        List<ProductDto> productDtos = null;
        try {
            items = itemMapper.selectItemFront(request.getCid(), column, dir, request.getPriceGt(), request.getPriceLte());
            itemPageInfo = new PageInfo<>(items);
            productDtos = productConverter.items2Dto(itemPageInfo.getList());
        } catch (Exception e) {
            log.error("ProductServiceImpl.getAllProduct Exception:" + e);
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);
        }
        allProductResponse.setTotal(itemPageInfo.getTotal());
        allProductResponse.setProductDtoList(productDtos);
        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();
        recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        try {
            List<Panel> panels = panelMapper.selectPanelContentById(GlobalConstants.RECOMMEND_PANEL_ID);
            if (panels == null || panels.isEmpty()) {
                return recommendResponse;
            }
            HashSet<PanelDto> set = new HashSet<>();
            panels.stream().forEach(panel -> {
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                PanelDto panelDto = contentConverter.panen2Dto(panel);
                panelDto.setPanelContentItems(panelContentItemDtos);
                set.add(panelDto);
            });
            recommendResponse.setPanelContentItemDtos(set);
        } catch (Exception e){
            log.error("ProductServiceImpl.getRecommendGoods Exception:" + e);
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse,e);
        }
        return recommendResponse;
    }
}
